package selection

/**
 * @Author: zhangQi
 * @Date: 2020-09-19 14:49
 */

fun main(){
    executeTest(arrayListOf(0, 2, 5, 1, 8, 23, 31, 21, 93, 213, 31, 11, 1512, 231, 341, 516, 132))
}

fun executeTest(list:ArrayList<Int>){
    print(list.selectionSort())
}

fun ArrayList<Int>.selectionSort():ArrayList<Int> {
    //获取Array Int型
    val sortedArray = arrayListOf<Int>()
    //遍好历
    for(i in 0 until size){
        val smallest = getSmallest()
        /**
         * 一个一个最小的添加到数组中
         */
        sortedArray += this[smallest]
        removeAt(smallest)
    }
    return sortedArray
}

fun ArrayList<Int>.getSmallest():Int{
    //this即获取了调用对象ArrayList<Int>
    var smallest = this[0]
    var index = 0
    for(i in 1 until size) {
        if(this[i] < smallest){
            //if this index number less than the option smallest
            //Certainly, set new smallest number to this[i]
            smallest = this[i]
            index = i
        }
    }
    return index;
}